﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ORM.Business.Interfaces;
using ORM.Business.Models;
using ORM.Business.Models.Entities;
using ORM.ViewModels.Guides;
using ORM.ViewModels.Home;
using ORM.ViewModels.Questions;

namespace ORM.Controllers
{
    public class HomeController : OrmControllerBase
    {
        private IQuestionRepository _iQuestionRepository;
        private IGuideRepository _iGuideRepository;
        private IUserRepository _iUserRepository;
        private OrmEntities ORMDbContext = new OrmEntities();
        public HomeController(IQuestionRepository iQuestionRepository, IGuideRepository iGuideRepository, IUserRepository iUserRepository)
        {
            _iQuestionRepository = iQuestionRepository;
            _iGuideRepository = iGuideRepository;
            _iUserRepository = iUserRepository;
        }
        public ActionResult Index()
        {
            var guideFeedback = _iGuideRepository.GetTopNewGuides();
            var questionFeedback = _iQuestionRepository.GetTopNewQuestions();
            var userFeedback = _iUserRepository.GetTopUsers();
            if(guideFeedback.Success && questionFeedback.Success && userFeedback.Success)
            {
                HomePageViewModel vm = new HomePageViewModel
                                       {
                                           TopNewGuides = guideFeedback.Data,
                                           TopNewQuestions = questionFeedback.Data,
                                           TopUsers = userFeedback.Data
                                       };
                return View(vm);
            }            
            return View();
        }

        public ActionResult About()
        {
            return View();
        }
        public int GetNotificationNumber()
        {
            int total = 0;
            if(CurrentUser != null)
            {
                var getCommentNotificationQuery = from c in ORMDbContext.GuideComments
                                                  join g in ORMDbContext.Guides on c.GuideId equals g.Id
                                                  orderby c.PostedDate descending
                                                  where
                                                      !c.IsDeleted && !g.IsDeleted &&
                                                      g.PostedBy == CurrentUser.UserInfo.Id &&
                                                      c.PostedBy != CurrentUser.UserInfo.Id &&
                                                      !c.IsReadByGuideAuthor
                                                  select c;
                int commentNotificationNumber = getCommentNotificationQuery.Count();
                var getAnswerNotificationQuery = from a in ORMDbContext.Answers
                                                  join q in ORMDbContext.Questions on a.QuestionId equals q.Id
                                                  orderby a.PostedDate descending
                                                  where
                                                      !a.IsDeleted && a.IsApproved && !q.IsDeleted &&
                                                      q.PostedById == CurrentUser.UserInfo.Id &&
                                                      a.PostedById != CurrentUser.UserInfo.Id &&
                                                      !a.IsReadByQuestioner
                                                  select a;
                int answerNotificationNumber = getAnswerNotificationQuery.Count();
                total = commentNotificationNumber + answerNotificationNumber;
            }
            return total;
        }
        public ActionResult SearchContent(string search)
        {
            QuestionListViewModel questionListViewModel = null;
            var qPageInfo = new PageInfo(Convert.ToInt16(ConfigurationManager.AppSettings["QuestionsPageSize"]), 1);
            var qFeedback = _iQuestionRepository.GetAllQuestion(qPageInfo, 0, 0, 0, search);
            if (qFeedback.Success)
            {
                questionListViewModel = new QuestionListViewModel
                {
                    Questions = qFeedback.Questions,
                    PageNo = 1,
                    Condition = "new",
                    DeviceId = 0,
                    ManufacturerId = 0,
                    ModelId = 0,
                    QuestionSearch = search,
                    TotalPage = Convert.ToInt16(Math.Ceiling((double)qFeedback.Total / qPageInfo.PageSize)),
                };
            }

            GuideListViewModel guideListViewModel = null;
            var pageInfo = new PageInfo(Convert.ToInt16(ConfigurationManager.AppSettings["GuidesPageSize"]), 1);
            var feedback = _iGuideRepository.GetAllGuide(pageInfo, 0, 0, 0, search, 0);
            if (feedback.Success)
            {
                guideListViewModel = new GuideListViewModel
                {
                    Guides = feedback.Guides,
                    PageNo = 1,
                    Condition = "new",
                    DeviceId = 0,
                    ManufacturerId = 0,
                    ModelId = 0,
                    GuideSearch = search,
                    TotalPage = Convert.ToInt16(Math.Ceiling((double)feedback.Total / pageInfo.PageSize)),
                };
            }

            SearchContentViewModel searchContentViewModel = null;
            if(qFeedback.Success && feedback.Success)
            {
                searchContentViewModel = new SearchContentViewModel
                                             {
                                                 QuestionListViewModel = questionListViewModel,
                                                 GuideListViewModel = guideListViewModel,
                                             };
                return View(searchContentViewModel);
            }
            return View();
        }
        public ActionResult GetAllDeviceList()
        {
            var Devices = ORMDbContext.Devices.Where(d => !d.IsDeleted).OrderBy(d => d.Name);
            return Json(Devices.ToList(), JsonRequestBehavior.AllowGet);
        }
        public ActionResult GetManufacturerList(int deviceId)
        {
            var queryString = "select distinct mf.* from Manufacturers as mf inner join Models as m on mf.Id = m.ManufacturerId where m.DeviceId = {0} and mf.IsDeleted = 'false'";
            var Manufacturers = (ORMDbContext as System.Data.Entity.Infrastructure.IObjectContextAdapter)
                              .ObjectContext.ExecuteStoreQuery<Manufacturer>(queryString, deviceId).ToList();
            return Json(Manufacturers.ToList(), JsonRequestBehavior.AllowGet);
        }
    }
}
